package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjkyqdBaDetailBo;
import com.ruoyi.report_sj.domain.vo.ReportSjkyqdBaDetailVo;
import com.ruoyi.report_sj.domain.ReportSjkyqdBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjkyqdBaDetailMapper;
import com.ruoyi.report_sj.service.IReportSjkyqdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆抗压强度试验检测记录详情Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjkyqdBaDetailServiceImpl implements IReportSjkyqdBaDetailService {

    private final ReportSjkyqdBaDetailMapper baseMapper;

    /**
     * 查询砂浆抗压强度试验检测记录详情
     */
    @Override
    public ReportSjkyqdBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询砂浆抗压强度试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSjkyqdBaDetailVo> queryPageList(ReportSjkyqdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjkyqdBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSjkyqdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆抗压强度试验检测记录详情列表
     */
    @Override
    public List<ReportSjkyqdBaDetailVo> queryList(ReportSjkyqdBaDetailBo bo) {
        LambdaQueryWrapper<ReportSjkyqdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjkyqdBaDetail> buildQueryWrapper(ReportSjkyqdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjkyqdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSjkyqdBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBianh() != null, ReportSjkyqdBaDetail::getBianh, bo.getBianh());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSjkyqdBaDetail::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getQuybw()), ReportSjkyqdBaDetail::getQuybw, bo.getQuybw());
        lqw.eq(bo.getZhijrq() != null, ReportSjkyqdBaDetail::getZhijrq, bo.getZhijrq());
        lqw.eq(bo.getShiyrq() != null, ReportSjkyqdBaDetail::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSjkyqdBaDetail::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportSjkyqdBaDetail::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getChengymj()), ReportSjkyqdBaDetail::getChengymj, bo.getChengymj());
        lqw.eq(bo.getJixzh() != null, ReportSjkyqdBaDetail::getJixzh, bo.getJixzh());
        lqw.eq(bo.getHuansxs() != null, ReportSjkyqdBaDetail::getHuansxs, bo.getHuansxs());
        lqw.eq(bo.getKangyqdcz() != null, ReportSjkyqdBaDetail::getKangyqdcz, bo.getKangyqdcz());
        lqw.eq(bo.getPingjkyqd() != null, ReportSjkyqdBaDetail::getPingjkyqd, bo.getPingjkyqd());
        return lqw;
    }

    /**
     * 新增砂浆抗压强度试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSjkyqdBaDetailBo bo) {
        ReportSjkyqdBaDetail add = BeanUtil.toBean(bo, ReportSjkyqdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改砂浆抗压强度试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSjkyqdBaDetailBo bo) {
        ReportSjkyqdBaDetail update = BeanUtil.toBean(bo, ReportSjkyqdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjkyqdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆抗压强度试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
